using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_editset : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(!IsPostBack) {
			if(Session.Count > 0 
			&& ap.CheckSessionExist(Session["Zappr"].ToString())) {
				if(Request.QueryString.Get("set") != null) {
					SetItem set = ip.GetSet(Request.QueryString.Get("set").ToString(), true);
					Account acc = ap.GetAccount(set.UserID);
					Account tmp = ap.GetAccountBySession(Session["Zappr"].ToString());

					if(acc.ID == tmp.ID) {
						plcEdit.Visible = true;
						
						if(set.Thumbnail == -1) {
							thumb.ImageUrl = "~/App_Themes/Default/Images/placeholder.jpg";
						}
						else {
							ImageItem mainimg = ip.GetImage(Convert.ToInt64(set.Thumbnail));
							thumb.ImageUrl = "~/Images/" + mainimg.Image240;
						}

						foreach(ImageItem ii in set.Images) {
							ListItem li = new ListItem();

							li.Text = ii.Name;
							li.Value = ii.Image240 + ii.ImageID;

							this.ddlImage.Items.Add(li);
						}

						ddlImage.Items.Insert(
							0,
							new ListItem(GetLocalResourceObject("Select").ToString(), "../App_Themes/Default/Images/placeholder.jpg")
						);

						ddlImage.Attributes.Add(
							"onchange", 
							thumb.ClientID.ToString() + ".src='Images/' + this.value.substring(0, 44);"
						);

						txtTitle.Text = set.Name;
						txtDesc.Text += set.Description;

						rblPrivacy.SelectedValue = set.Access.ToString();
					}
					else {
						Response.Redirect("~/login.aspx");
					}
				}
				else {
					Response.Redirect("~/default.aspx");
				}
			}
			else {
				Response.Redirect("~/login.aspx");
			}
		}
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			SetItem item = ip.GetSet(Request.QueryString.Get("set").ToString());

			item.Access = Convert.ToInt32(rblPrivacy.SelectedValue);
			item.Name = txtTitle.Text.Trim();
			item.Description = txtDesc.Text.Trim();

			if(ddlImage.SelectedIndex != 0) {
				item.Thumbnail = -1;
				item.Thumbnail = Convert.ToInt64(ddlImage.SelectedValue.Substring(44));
			}

			ip.SaveSet(item);

			Response.Redirect("~/default.aspx?set=" + item.SetID.ToString());
			Response.Redirect("~/set/" + item.SetID.ToString() + "/default.aspx");
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());

			//Response.Redirect("~/default.aspx?id=sets&user=" + acc.Username);
			Response.Redirect("~/sets/" + acc.Username + "/default.aspx");
		}
		else {
			Response.Redirect("~/login.aspx");
		}
	}
}
